package com.neuedu.his.common.utils;

import com.neuedu.his.entity.Dept;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


public class ParamUtils {

    //String  --Integer
    //String -- Long
    //String   Double
    //String( 1701997963000  \2021-01-01       2021/21/01          2021/21/01 15:59:01  2021-01-01 15:59:01    )   Date

    //注册一个日期转换器
    static{
        ConvertUtils.register(new Converter() {
            @Override
            public <T> T convert(Class<T> type, Object value) {

                if(value== null || "".equals(value)){
                    return null;
                }
                List<SimpleDateFormat> list = new ArrayList<>();
                list.add(new SimpleDateFormat("yyyy-MM-dd"));
                list.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
                list.add(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"));
                list.add(new SimpleDateFormat("yyyy/MM/dd "));

                if(value instanceof  String){
                    String paramsValue = (String) value;

                    for (SimpleDateFormat sdf : list) {
                        try {
                            Date parse = sdf.parse(paramsValue);
                            return (T) parse;
                        } catch (ParseException e) {
//                           e.printStackTrace();
                        }
                    }


                    //1701997963000
                    return (T) new Date(Long.valueOf(paramsValue));

                }
                return null;
            }
        }, Date.class);
    }


    public static <T> T getParam(Class<T> clazz , HttpServletRequest request) {


        Object obj = null;
        try {
            obj = clazz.newInstance();
            //从request中获取参数
            Map map = request.getParameterMap();
            //遍历所有的 key
            /***
             * username:
             * 11212
             * password:
             * 12
             * nickname:
             * 12
             * deptId:
             * 1
             * deptName:

             * expire:
             */
            //清楚 没有值的参数

            Set<Map.Entry<String,Object[]>> set = map.entrySet();

            Map paramMap = new HashMap();
            for (Map.Entry<String,Object[]> entry : set) {
                Object[] value = (Object[]) entry.getValue();
                if(value == null || "".equals(value)   || (value.length == 1 && (value[0] == null || "".equals(value[0]) ) ) ){
                }else{ //只放有  value 对应的 参数
                    paramMap.put(entry.getKey(),entry.getValue());
                }
            }

            BeanUtils.populate(obj ,paramMap);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        return (T) obj;
    }
}
